import { _decorator, Button, color, Color, Label, Sprite, UITransform } from 'cc';
import { Ab_BasePopUp_bA } from '../../../A-FRAME/component/ui.pop-up';
import { Ab_ClientAPI_bA } from '../../api/client.api';
import Ab_GameDC_bA from '../../manager/data.game.center';
import Ab_GameAssets_bA from '../../manager/game.assets';
import { Ab_Float_bA } from '../page/float';
import type { Ab_Popup_bA } from '../page/popup';
const { ccclass, property } = _decorator;

@ccclass('Ab_Prop_bA')
export class Ab_Prop_bA extends Ab_BasePopUp_bA<typeof Ab_Popup_bA, 'video'|'diamond'|'not'> {
	protected Ab_default_return_bA: 'not';

	private Ab_ui_icon_bA: UITransform;
	private Ab_lbl_desc_bA: Label;
	private Ab_prop_type_bA: 'stash' | 'revoke' | 'shuffle';

	protected Ab_initAfterOnLoad_bA(){
		this.Ab_ui_icon_bA = this.node.getChildByName('icon').getComponent(UITransform);
		this.Ab_lbl_desc_bA = this.node.getChildByName('lbl-desc').getComponent(Label);
		this.Ab_renderLocalUI_bA();
	}

	protected Ab__click_event__bA: { [name: string]: (this: Ab_Prop_bA, button: Button) => void; } = {
		close(){
			this.Ab_enableAllButton_bA(false);
			this.Ab_exit_bA('not').then(()=>this.Ab_enableAllButton_bA(true));
		},
		async video(){
			this.Ab_enableAllButton_bA(false);
			if(this.Ab_used_list_bA.includes('video')){
				await Ab_Float_bA.Ab_Hint_bA(Ab_GameAssets_bA.Ab_getLangText_bA(61));
			}else{
				await Ab_ClientAPI_bA.Ab_AD_bA('incentive');
				await this.Ab_exit_bA('video');
			}
			this.Ab_enableAllButton_bA(true);
		},
		async diamond(){
			this.Ab_enableAllButton_bA(false);
			if(this.Ab_used_list_bA.includes('diamond')){
				await Ab_Float_bA.Ab_Hint_bA(Ab_GameAssets_bA.Ab_getLangText_bA(61));
			}else{
				let enough = Ab_GameDC_bA.Ab_User_bA.Ab_diamond_bA >= 100;
				if(enough){
					Ab_GameDC_bA.Ab_Trigger_bA.Ab_CHANGE_DIAMOND_bA = -100;
					await this.Ab_exit_bA('diamond');
				}else{
					await Ab_Float_bA.Ab_Hint_bA(Ab_GameAssets_bA.Ab_getLangText_bA(65));
				}
			}
			this.Ab_enableAllButton_bA(true);
		}
	}

	private Ab_used_list_bA: Array<'diamond'|'video'> = [];
	public Ab_onShow_bA(prop: 'stash' | 'revoke' | 'shuffle', used_list?: Array<'diamond'|'video'>){
		this.Ab_used_list_bA = used_list ?? [];
		this.Ab_prop_type_bA = prop;
		this.Ab_refreshButton_bA();
		abi.Ab_cc_bA.Ab_skinPeeler_bA(this.Ab_ui_icon_bA.node, this.Ab_smap_bA.get('icon-prop-'+prop), {height: true});
		this.Ab_lbl_desc_bA.string = Ab_GameAssets_bA.Ab_getLangText_bA({stash: 27, revoke: 26, shuffle: 25}[prop]);
		Ab_BasePopUp_bA.Ab_jelly_enter_bA(this.node);
	}

	private async Ab_exit_bA(type: 'video'|'diamond'|'not'){
		if(type != 'not') Ab_Float_bA.Ab_addProp_bA(this.Ab_prop_type_bA, this.Ab_ui_icon_bA.node);
		await Ab_BasePopUp_bA.Ab_jelly_leave_bA(this.node);
		this.Ab_close_bA(type);
	}

	private Ab_renderLocalUI_bA(){
		this.node.getChildByName('lbl-diamond').getComponent(Label).string = Ab_GameAssets_bA.Ab_getLangText_bA(23, 100);
		this.node.getChildByName('lbl-video').getComponent(Label).string = Ab_GameAssets_bA.Ab_getLangText_bA(24);
	}

	private Ab_refreshButton_bA(){
		let gray = Color.fromHEX(color(), '#7C7C7C');
		let out_color_video = Color.fromHEX(color(), '#367423');
		let out_color_diamond = Color.fromHEX(color(), '#005C9E');
		(<const>['video', 'diamond']).forEach(name=>{
			['', 'icon-'].forEach(prefix=>this.node.getChildByName(prefix+name).getComponent(Sprite).grayscale = this.Ab_used_list_bA.includes(name));
		});

		this.node.getChildByName('lbl-video').getComponent(Label).outlineColor = this.Ab_used_list_bA.includes('video') ? gray : out_color_video;
		this.node.getChildByName('lbl-diamond').getComponent(Label).outlineColor = this.Ab_used_list_bA.includes('diamond') ? gray : out_color_diamond;
	}

	private Ab_enableAllButton_bA(enable: boolean){
		['video', 'diamond', 'close']
		.forEach(name=>this.Ab___btn_map___bA.get(name).interactable = enable);
	}
}

